<template>
  <template v-if="variableType === 'entity'">
    <el-table :data="value" border max-height="400" style="width: 100%;">
      <el-table-column prop="id" type="index" label="序号" width="60"></el-table-column>
      <template v-if="params?.entityConfig?.fields"
                v-for="(field, fieldName) in params?.entityConfig?.fields">
        <el-table-column :prop="fieldName" :label="field.desc" min-width="160" sortable />
      </template>
      <template v-if="value?.length > 0" v-for="(field, fieldName) in value[0]">
        <template v-if="!params?.entityConfig?.fields[fieldName]">
          <el-table-column :prop="fieldName" :label="fieldName" min-width="160" sortable />
        </template>
      </template>
    </el-table>
  </template>
  <template v-else-if="variableType === 'newProcesses'">
    <el-table :data="value" border max-height="400" style="width: 100%;">
      <el-table-column prop="id" type="index" label="序号" width="60"></el-table-column>
      <template v-if="value?.length > 0" v-for="(field, fieldName) in value[0]">
        <el-table-column :prop="fieldName" :label="fieldName" min-width="160" sortable />
      </template>
    </el-table>
  </template>
  <template v-else>
    {{ value }}
  </template>
</template>

<script lang="ts" setup>
import {CategoryVariable} from "@/core/entity/CategoryProp";

const {modelValue, variableConfig} = defineProps(['modelValue', 'variableConfig']);
const showFieldValue: any = (variableConfig: CategoryVariable, rowData: any): string => {
  let variableName = variableConfig.variableName;
  if (variableName.endsWith("Id")) {
    variableName = variableName.substring(0, variableName.lastIndexOf("Id")) + "Name";
  } else if (variableName.endsWith("Ids")) {
    variableName = variableName.substring(0, variableName.lastIndexOf("Ids")) + "Name";
  } else if (variableConfig.variableType !== 'string' && variableConfig.variableType !== 'list') {
    variableName = variableName + "Name";
  }
  if (Object.hasOwn(rowData.root, variableName)) {
    return rowData.root[variableName];
  }
  return rowData.root[variableConfig.variableName];
}
const variableType = variableConfig.variableType;
const params = variableConfig.params;
const value = showFieldValue(variableConfig, modelValue);

</script>

<script lang="ts">
import {CategoryVariable} from "@/core/entity/CategoryProp";

export default {
  name: "DescriptionCell",
  props: {modelValue: null, variableConfig: {type: CategoryVariable}}
}
</script>

<style scoped>

</style>